package com.yugong.common;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 请求结果响应信息
 *
 * @author yugong
 * @date 2024/2/9
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result {
    // 响应码
    private String code;
    // 响应信息
    private String msg;
    // 响应数据
    private Object data;

    private Result(Object data) {
        this.data = data;
    }

    /**
     * 无参数请求成功
     *
     * @return 请求成功
     */
    public static Result success() {
        Result result = new Result();
        result.setCode("200");
        result.setMsg("请求成功");
        return result;
    }

    /**
     * 有参数请求成功
     *
     * @param data 数据
     * @return 请求成功
     */
    public static Result success(Object data) {
        Result result = success();
        result.setData(data);
        return result;
    }

    /**
     * 无错误信息请求失败
     *
     * @return 请求失败
     */
    public static Result error() {
        Result result = new Result();
        result.setCode("500");
        result.setMsg("请求失败");
        return result;
    }

    /**
     * 有错误信息请求失败
     *
     * @param data 数据
     * @param msg  错误信息
     * @return 请求失败
     */
    public static Result error(Object data, String msg) {
        Result result = new Result();
        result.setCode("500");
        result.setData(data);
        result.setMsg(msg);
        return result;
    }
}
